package com.panopset.sf;

import java.awt.GridLayout;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.JTextComponent;

import com.panopset.compat.Strings;

/**
 * Titled text area.
 *
 * @author Karl Dinwiddie
 *
 */
public final class TitledTextArea {

    /**
     * Text area.
     */
    private final JTextComponent ta;

    /**
     * Scroll pane.
     */
    private final JScrollPane sp;

    /**
     * Panel.
     */
    private final JPanel p;

    /**
     *
     * @return Titled panel containing scrollpane and text area.
     */
    public JPanel getPanel() {
        return p;
    }

    /**
     *
     * @return Text area.
     */
    public JTextArea getTextArea() {
        return (JTextArea) ta;
    }

    /**
     * @param w
     *            Windoc.
     * @param title
     *            Title.
     * @param tip
     *            Can be null.
     * @param dft
     *            Default.
     * @param key
     *            Persistent store key. If the key is empty, a key will be
     *            generated based on the stack trace. If the key is null, there
     *            will be no registration.
     */
    TitledTextArea(final Windoc w, final String title,
            final String tip, final String dft, final String key) {
    	ComponentFactory cf = new ComponentFactory();
        p = cf.createTitledPanel(new GridLayout(1, 1), title);
        ta = cf.registerTextComponent(w, new JTextArea(), dft, key);
        sp = new JScrollPane(ta);
        p.add(sp);
        if (Strings.isPopulated(tip)) {
            p.setToolTipText(tip);
            ta.setToolTipText(tip);
            sp.setToolTipText(tip);
        }
    }

    /**
     * @param w
     *            Windoc.
     * @param title
     *            Title.
     * @param tip
     *            Can be null.
     * @param key
     *            Persistent store key. If the key is empty, a key will be
     *            generated based on the stack trace. If the key is null, there
     *            will be no registration.
     */
    TitledTextArea(final Windoc w, final String title,
            final String tip, final String key) {
        this(w, title, tip, "", key);
    }

    /**
     * Append.
     *
     * @param s
     *            String to append.
     */
    public void append(final String s) {
        ((JTextArea) getTextArea()).append(s);
    }

    /**
     *
     * @param listener
     *            Key listener.
     */
    public void addKeyListener(final KeyListener listener) {
        getTextArea().addKeyListener(listener);
    }

    /**
     * @return value.
     */
    public String getText() {
        return getTextArea().getText();
    }

    /**
     * s * @param value Value to set.
     */
    public void setText(final String value) {
        getTextArea().setText(value);
    }
}
